<template>
  <el-dialog
    :destroy-on-close="true"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="role_name">
        <el-input v-model="form.role_name" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          v-model="form.sort"
          :step="1"
          step-strictly
          @change="numberChange"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="form.status"
          active-text="显示"
          :active-value="1"
          inactive-text="隐藏"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="权限菜单">
        <div class="vab-tree-border">
          <el-tree
            ref="tree"
            :data="list"
            :default-checked-keys="role"
            node-key="id"
            :props="defaultProps"
            show-checkbox
            @check="handleCheck"
          >
            <template #default="{ data }">
              <span>{{ data.menu_name }}</span>
            </template>
          </el-tree>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit, addRole } from '@/api/role'
  import { getTree } from '@/api/rule'

  export default {
    name: 'RoleEdit',
    data() {
      return {
        defaultProps: {
          children: 'sub_menu',
          label: 'menu_name',
        },
        form: {
          role_name: '',
          roles: '',
          status: 1,
          sort: 0,
        },
        role: [],
        rules: {
          role: [{ required: true, trigger: 'blur', message: '请输入角色码' }],
        },
        title: '',
        dialogFormVisible: false,
        list: [],
      }
    },
    methods: {
      handleCheck(data, check) {
        this.role = check.checkedKeys
      },
      numberChange(value) {
        this.form.sort = value
        this.$forceUpdate()
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form.role_id = row.id
          this.form.role_name = row.role_name
          this.form.status = row.role_status
          this.form.sort = row.role_sort
          this.role = row.rules.split(',')
          this.$forceUpdate()
        }
        this.dialogFormVisible = true
        this.fetchData()
        this.$forceUpdate()
      },
      close() {
        this.$refs['form'].resetFields()
        this.role = []
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      async fetchData() {
        const {
          data: { items },
        } = await getTree({})
        this.list = items
      },
      save() {
        // 具体业务请自行调整
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title == '添加') {
              this.form.roles = this.role.join(',')
              const { data } = await addRole(this.form)
              if (data.is_success) {
                this.$baseMessage(
                  '添加成功',
                  'success',
                  'vab-hey-message-success'
                )
              }
            } else if (this.title == '编辑') {
              this.form.roles = this.role.join(',')
              const { data } = await doEdit(this.form)
              if (data.is_success) {
                this.$baseMessage(
                  '修改成功',
                  'success',
                  'vab-hey-message-success'
                )
              }
            }
            this.$emit('fetch-data')
            this.close()
            // await location.reload()
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .vab-tree-border {
    height: 200px;
    padding: $base-padding;
    overflow-y: auto;
    border: 1px solid #dcdfe6;
    border-radius: $base-border-radius;
  }
</style>
